//
//  EyeCareManager.swift
//  HeadlineNews
//
//  Created by han on 2019/5/30.
//  Copyright © 2019 han. All rights reserved.
//

import UIKit

let kEyeCareWindowLevel = 2099

class EyeCareManager: NSObject {
    
    static let shared = EyeCareManager()
    private override init() {}
    
    private var level: CGFloat = 0
    private var before: CGFloat = 0
    
    private weak var  priviousKeyWindow: UIWindow?
    private var eyeCareWindow: EyeCareWindow?
    
    func setEyeCare(level: CGFloat) {
        if self.level == level {
            return
        }
        self.before = self.level
        self.level = level
        if self.before != 0 && self.level == 0 {
            dismissEyeCareWindow()
        }else {
            showEyeCareWindow()
        }
    }
    
    private func addEyeCareWindow() {
        let window = EyeCareWindow.init(frame: UIScreen.main.bounds)
        window.isUserInteractionEnabled = false
        window.windowLevel = UIWindow.Level.init(CGFloat(kEyeCareWindowLevel))
        self.eyeCareWindow = window
        
        self.priviousKeyWindow = UIApplication.shared.keyWindow
    }
    
    private func showEyeCareWindow() {
        if self.eyeCareWindow == nil {
            addEyeCareWindow()
        }
        
        self.priviousKeyWindow?.isUserInteractionEnabled = false
        self.eyeCareWindow?.makeKey()
        self.eyeCareWindow?.isHidden = false
        let opacityAnimation = creatAnimation(from: before, to: level)
        self.eyeCareWindow?.layer.add(opacityAnimation, forKey: "showEyeCareWindow")
    }
    private func dismissEyeCareWindow() {
        guard (self.eyeCareWindow != nil) && UIApplication.shared.windows.contains(self.eyeCareWindow!) else { return }
        
        self.priviousKeyWindow = UIApplication.shared.keyWindow
        self.priviousKeyWindow?.isUserInteractionEnabled = false
        let opacityAnimation = creatAnimation(from: before, to: level)
        self.eyeCareWindow?.layer.add(opacityAnimation, forKey: "dismissEyeCareWindow")
    }
    private func creatAnimation(from: CGFloat, to: CGFloat) -> CABasicAnimation {
        let opacityAnimation = CABasicAnimation(keyPath: "opacity")
        opacityAnimation.duration = 0.5
        opacityAnimation.fromValue = from
        opacityAnimation.toValue = to
        opacityAnimation.timingFunction = CAMediaTimingFunction.init(name: .easeInEaseOut)
        opacityAnimation.fillMode = .forwards
        opacityAnimation.isRemovedOnCompletion = false
        opacityAnimation.delegate = self
        return opacityAnimation
    }
    
}

extension EyeCareManager: CAAnimationDelegate {
    
    func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
        if anim.isEqual(self.eyeCareWindow?.layer.animation(forKey: "showEyeCareWindow")) {
            self.priviousKeyWindow?.makeKey()
            self.priviousKeyWindow?.isUserInteractionEnabled = true
            self.priviousKeyWindow = nil
        }else if  anim.isEqual(self.eyeCareWindow?.layer.animation(forKey: "dismissEyeCareWindow")) {
            self.eyeCareWindow?.isHidden = true
            self.priviousKeyWindow?.isUserInteractionEnabled = true
            self.priviousKeyWindow = nil
        }
    }
}

class EyeCareCoverLayer: CALayer {
    override init() {
        super.init()
        setupLayer()
    }
    override init(layer: Any) {
        super.init()
        setupLayer()
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupLayer()
    }
    
    func setupLayer() {
        self.opacity = 0.0
        self.backgroundColor = UIColor.black.cgColor
    }
}

class EyeCareWindow: UIWindow {
    override class var layerClass : AnyClass {
        return EyeCareCoverLayer.self
    }
}
